package com.lz.football.module.compete
{
	import com.common.utils.Reflection;
	import com.liangzhong.constants.ClubConst;
	import com.liangzhong.events.AppEventDispatcher;
	import com.liangzhong.events.CompeteEvent;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.compete.CompeteDataManager;
	import com.lz.football.module.compete.item.RankItem;
	
	import common.BaseView.BaseView;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import lz.controls.Alert;
	
	import mx.effects.Move;

	/**
	 * 国王选择挑战面板挑战 
	 * @author Administrator
	 * 
	 */	
	public class ChallengeView extends BaseView
	{
		/**
		 *国家ID 
		 */
		private var _countryId:int;
		/**
		 *国家名称  
		 */
		private var country:TextField;
		/**
		 *挑战按钮 
		 */
		private var challengeBtn:SimpleButton;
		/**
		 * 国家图标  
		 */
		private var countryIcon:MovieClip;
		/**
		 * 
		 */
		private var countrydes:TextField;
		/**
		 * 
		 */
		private var matchName:TextField;
		/**
		 *buffer 
		 */
		private var bufferDes:TextField;
//		private var kingMc:movi
		/**
		 * 
		 */
		private var buffer:MovieClip;
		
		private var rankItemVec:Vector.<RankItem>;
		
		public function ChallengeView()
		{
			super();
		}
		override public function initUI(view:Object=null):void
		{
			symbolSkin = Reflection.createInstance("com.lz.modul.compete.challengePanel");
			this.addChild(symbolSkin);
			symbolSkin.cacheAsBitmap = true;
			_viewHeight = symbolSkin.height;
			_viewWidth = symbolSkin.width;
			_windowBg = symbolSkin.bg;
			country = symbolSkin.country;
			challengeBtn = symbolSkin.challengeBtn;
			countryIcon = symbolSkin.countryIcon;
			bufferDes = symbolSkin.bufferDes;
			countrydes = symbolSkin.countrydes;
			matchName = symbolSkin.matchName;
			buffer = symbolSkin.buffer;
			isDrag = true;
			isCloseBtn = true;
			moveClose(_viewWidth - 30,5);
			onResizeHandler();
			addEvtListener();
			initItem();
		}
		/**
		 * 
		 * 
		 */
		private function initItem():void
		{
			rankItemVec = new Vector.<RankItem>();
			var item:RankItem;
			for(var i:int = 0;i < 4;i++)
			{
				item = new RankItem();
				rankItemVec.push(item);
				if(i > 0)
				{
					item.x = 80 * (i -1) + 385;
					item.y = 256;
				}
				this.addChild(item);
			}
			rankItemVec[0].x = 456;
			rankItemVec[0].y = 162;
		}
		/**
		 *选择了国家 
		 * @param $country
		 * 
		 */
		public function selectCountry($country:int):void
		{
			_countryId = $country;
			countryIcon.gotoAndStop(_countryId);
			country.text = ClubConst.countryToString(_countryId);
			countrydes.text = CompeteDataManager.getCountryDes(_countryId);
			matchName.text = matchDes(_countryId);
		}
		private function matchDes($co:int):String
		{
			switch($co)
			{
				case ClubConst.COUNTRY_ENGLAND:
					return "英超";
					break;
				case ClubConst.COUNTRY_SPAIN:
					return "西班牙";
					break;
				case ClubConst.COUNTRY_FRANCE:
					return "法甲";
					break;
				case ClubConst.COUNTRY_GERMANY:
					return "德甲";
					break;
				case ClubConst.COUNTRY_ITALY:
					return "意甲";
					break;
				case ClubConst.COUNTRY_CHAIN:
					return "中超";
					break;
			}
			return "";
		}
		
		/**
		 * 设置数据 
		 * @param $obj
		 * 
		 */
		public function setData($obj:Object):void
		{
			bufferDes.htmlText = CompeteDataManager.getbufferTipsIsCountry($obj.nationRank);
//			trace($obj.nationRank);
			buffer.gotoAndStop(int($obj.nationRank));
			var arr:Array = $obj.arr;
			var len:int = arr.length > 4?4:arr.length;
			for(var i:int = 0;i < len;i++)
			{
				rankItemVec[i].reset();
				rankItemVec[i].setData(arr[i]);
			}
			
		}
		override public function addEvtListener():void
		{
			challengeBtn.addEventListener(MouseEvent.CLICK,onChallengeHandler);
		}
		
		protected function onChallengeHandler(event:MouseEvent):void
		{
			// TODO Auto-generated method stub
			if(_countryId == AppData.getInstance().clubDatamanager.clubInfo.country)
			{
				Alert.showErrorTip("不可以挑战自己国家");
				return;
			}
//			if(AppData.getInstance().competeDataManager.competeInfoVo.king == 0)
//			{
//				Alert.showErrorTip("国王才可以选择挑战国家！");
//				return;
//			}
			AppEventDispatcher.getInstance().dispatchEvent(new CompeteEvent(CompeteEvent.KINGSELECTCHANGELLNATION,{id:_countryId}));
		}
		override public function removeEvtListener():void
		{
			challengeBtn.removeEventListener(MouseEvent.CLICK,onChallengeHandler);
			super.removeEvtListener();
		}
		override public function destroyView():void
		{
			removeEvtListener();
			while(rankItemVec.length)
				rankItemVec.pop().destroyView();
			rankItemVec = null;
			super.destroyView();
		}
	}
}